<?php
session_start();
if (isset($_SESSION['openid'])){
    include '../c/config.php';
    $mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
    
    $limitNum=20;
    $query=$mysqli->query("SELECT topnum FROM z_game where is_open='1' limit 0, 1");
    if($query->num_rows>0){
        $getLimit=$query->fetch_assoc();
        $limitNum=$getLimit['topnum'];
    }
    
    
    $userTid=0;
    $teamInfo=array();
    $queryG=$mysqli->query("select tid from ".$tableVer."_group where openid='".$_SESSION['openid']."' limit 0, 1");
    if($queryG->num_rows>0){
        $getG= $queryG->fetch_assoc();
        $userTid=$getG['tid'];
        $queryT=$mysqli->query("select name,fen from ".$tableVer."_team where id='".$userTid."' limit 0, 1");
        $getTeam=$queryT->fetch_assoc();
        $UserA=array();
        $queryUserA=$mysqli->query("SELECT 	headimg  FROM `".$tableVer."_group` where tid='".$userTid."' limit 0,3 ");
        while ($getG=$queryUserA->fetch_assoc()){
            $UserA[]=$getG['headimg'];
        }
        $teamInfo=array('n'=>$getTeam['name'],'f'=>$getTeam['fen'],'ua'=>$UserA);
    }
    
    $teamPai=array();
    $i=0;
    $userTeamPaiNum=0;
    $queryP=$mysqli->query("select id,name,fen,ishuojiang from ".$tableVer."_team where iswork=1 and renshu=3 order by fen desc limit 0, ".$limitNum);
    while ($list=$queryP->fetch_assoc()){
        $tid=$list['id'];
        $teamA=array();
        $queryG=$mysqli->query("SELECT 	headimg  FROM `".$tableVer."_group` where tid='".$tid."' limit 0,3 ");
        while ($getG=$queryG->fetch_assoc()){
            $teamA[]=$getG['headimg'];
        }
        $i++;
        if ($tid == $userTid){
            $userTeamPaiNum=$i;
        }
        if (1 == $list['ishuojiang']){
            $ishuojiang=1;
        }else{
            $ishuojiang=0;
        }
        
        $teamPai[]=array('hj'=>$ishuojiang,'f'=>$list['fen'],'n'=>$list['name'],'a'=>$teamA);
    }
 
    echo json_encode(array('id'=>$userTid, 'up'=>$userTeamPaiNum, 'u'=>$teamInfo,'t'=>$teamPai));
    
}
?>
